package tinglema.server.config;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author delucia
 * @version 1.0.0
 * @date 2025-05-12
 * @apiNote 阿里云oss对象存储配置类
 */
@Configuration
public class OssConfig {

    @Value("${aliyun.oss.endPoint}")
    private String ENDPOINT;
    @Value("${aliyun.oss.accessKeyId}")
    private String ACCESS_KEY_ID;
    @Value("${aliyun.oss.accessKeySecret}")
    private String ACCESS_KEY_SECRET;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    //注入一个OSS实例
    @Bean
    public OSS ossClient() {
        OSS ossClient =  new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        // try {
        //     // 设置存储空间的读写权限
        //     ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        // } catch (OSSException oe) {
        //     System.out.println("Caught an OSSException, which means your request made it to OSS, "
        //             + "but was rejected with an error response for some reason.");
        //     System.out.println("Error Message:" + oe.getErrorMessage());
        //     System.out.println("Error Code:" + oe.getErrorCode());
        //     System.out.println("Request ID:" + oe.getRequestId());
        //     System.out.println("Host ID:" + oe.getHostId());
        // } catch (ClientException ce) {
        //     System.out.println("Caught an ClientException, which means the client encountered "
        //             + "a serious internal problem while trying to communicate with OSS, "
        //             + "such as not being able to access the network.");
        //     System.out.println("Error Message:" + ce.getMessage());
        // } finally {
        //     if (ossClient != null) {
        //         ossClient.shutdown();
        //     }
        // }
        return ossClient;
    }

}
